package com.gxa.day2022_07_29_21.homework.order_system_4.service;

import com.gxa.day2022_07_29_21.homework.order_system_4.model.Dish;
import com.gxa.day2022_07_29_21.homework.order_system_4.dao.DishDao;
import com.gxa.day2022_07_29_21.homework.order_system_4.memory_store.MemoryStore;

import java.util.List;
import java.util.Scanner;


/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2022/7/30 15:27
 **/
public class DishService {
    private DishDao dishDao = new DishDao();


    /**
     * 查询所有菜单
     *
     * @return 菜单列表
     */
    public List<Dish> getDishes() {
        return MemoryStore.dishes;
    }

    /**
     * 打印菜单
     */
    public void printDishMenu() {
        List<Dish> dishes = dishDao.getDishes();
        System.out.println("序号\t 菜品\t 单价\t点赞数");
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            String price = dish.getDishPrice() + "元";
            String praiseNum = (dish.getPraiseNum()) > 0 ? dish.getPraiseNum() + "赞" : "";
            System.out.println((i + 1) + "\t" + dish.getDishName() + "\t" + price + "\t" + praiseNum);
        }
    }

    /**
     * 我要点赞
     */
    public void praise() {
        List<Dish> dishes = getDishes();
        Scanner scanner = new Scanner(System.in);

        System.out.println("***我要点赞***");
        printDishMenu();
        System.out.print("请选择你要点赞的菜品序号：");
        int praiseId = scanner.nextInt();

        if ((praiseId -1) > dishes.size() -1 || (praiseId -1) < 0) {
            throw new IllegalArgumentException("点赞失败，该菜品不存在！");
        }
        Dish dish = getDishByIndex(praiseId - 1);
        dish.setPraiseNum(dish.getPraiseNum() + 1);
        System.out.println("点赞成功！");
        printDishMenu();

    }


    /**
     * 根据索引下标查找Dish
     *
     * @param idx
     * @return
     */
    public Dish getDishByIndex(int idx) {
        return dishDao.getDishByIndex(idx);
    }
}
